/*
 * Copyright 2010 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.locationbasedagent;

import java.util.ArrayList;
import java.util.List;

import com.locationbasedagent.shared.HouseListingProxy;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;


public class ResultListActivity extends Activity implements AsyncTaskDelegate{
    /**
     * Tag for logging.
     */
    private static final String TAG = "SearchByListActivity";
    
    private Button mListResultButton;
    private Button mMapResultButton;
    private ListView listview;

	private List<HouseListingProxy> items = new ArrayList<HouseListingProxy>();

    /**
     * The current context.
     */
    private Context mContext = this;
    
    private ProgressDialog dialog = null;
    
    private AsyncFetchTask task;
    ResultListAdapter adapter;
    /**
     * 
     * Called when the activity is first created.
     */
    @Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		
		Bundle bundle = getIntent().getExtras();
		dialog = ProgressDialog.show(this, "Loading...", "Retrieving search results", true);
		setContentView(R.layout.resultlayout);
		listview = (ListView) findViewById(R.id.listView1);
		// Use your own layout
		adapter = new ResultListAdapter(this, items);
		
		String addressValue = bundle.getString("address");
		String cityValue = bundle.getString("city");
		String zipValue = bundle.getString("zip");
		String sellingID = bundle.getString("sellingID");
		fetchHouseListings(addressValue, cityValue, zipValue, sellingID);
		listview.setAdapter(adapter);
		
		listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
			   public void onItemClick(AdapterView parentView, View childView, int position, long id) {  
				   HouseListingProxy clickedHouse = (HouseListingProxy) adapter.getItem(position);
					Intent intent =
			            new Intent(ResultListActivity.this,
			                    HouseListingActivity.class);
					Bundle bundle = new Bundle();
			    
					bundle.putParcelable("listing", new LocalHouseListing(clickedHouse));
		    	    intent.putExtras(bundle); 
		    	    startActivity(intent);
	
			   }
		});  
		
	}


    
    
    public void fetchHouseListings(String address, String city, String zip, String sellingID) {
        if (task != null) {
            task.cancel(true);
        }
        task = new AsyncFetchTask(this);
        task.execute("", "", "", address, city, zip,
    			sellingID, "", "", "", "", "", "", 
    			"", "", "", "");
        
    }

    public void updateHouseListings(List<HouseListingProxy> listings) {
    	dialog.dismiss();
    	this.items.clear();
        if (listings.size() != 0)
        {
        	this.items.addAll(listings);
        	adapter.notifyDataSetChanged();
        }
    }
    
   

    
}